﻿#region Original Work By Marcelo Chavez
//---------------------------------------------------------------------------------------------------
//
//  This library is based on original work by Marcelo Chavez.
//  Please see http://www.codeproject.com/KB/miscctrl/lcars_net_controls.aspx
//  for more information.
//
//---------------------------------------------------------------------------------------------------
#endregion

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace Streambolics.Lcars
{
    // TODO: Make the button derive from the label, so the drawing of the text is done
    //       the same way.

    [ToolboxBitmap (@"C:\Inetpub\wwwroot\LCARS\LCARS_Toolbox\icon\st1_16.ico")]
    public class Button : Label
    {
        // Fields
        private bool _Blink = false;
        private bool _BlinkState = false;

        private string _SoundFile = "";
        private bool _IsDown = false;

        // TODO: Try to remove this.
        private IContainer components;

        private Sound lCARS_Sound1;

        // TODO: Each button uses its own timer, and thus blinks independantly of all
        //       the others. Do we really want that, or do we want all blinking to
        //       be synchronized?

        private Timer timer1;

        // Methods
        public Button ()
        {
            this.InitializeComponent ();
            base.SetStyle (ControlStyles.DoubleBuffer, true);
            base.SetStyle (ControlStyles.AllPaintingInWmPaint, true);
        }

        private void InitializeComponent ()
        {
            // BUG: Timer is not disposed of
            this.components = new Container ();
            this.timer1 = new Timer (this.components);
            this.lCARS_Sound1 = new Sound ();
            this.timer1.Interval = 0x3e8;
            this.timer1.Tick += new EventHandler (this.Timer1Tick);
            this.Cursor = Cursors.Hand;
            base.Name = "LCARS_Button";
            base.Size = new Size (0x68, 0x20);
        }

        protected override void OnMouseHover (EventArgs e)
        {
            base.OnMouseHover (e);
            _IsDown = true;
            Invalidate ();
        }

        protected override void OnMouseLeave (EventArgs e)
        {
            base.OnMouseLeave (e);
            _IsDown = false;
            Invalidate ();
        }

        protected override void OnMouseDown (MouseEventArgs e)
        {
            base.OnMouseDown (e);
            if (this._SoundFile.Trim () != "")
            {
                this.lCARS_Sound1.PlayOnce (this._SoundFile);
            }
        }

        protected override void OnPaint (PaintEventArgs e)
        {
            UseAlternateColor = _IsDown || (_Blink && _BlinkState);
            base.OnPaint (e);
        }

        private void Timer1Tick (object sender, EventArgs e)
        {
            _BlinkState = !_BlinkState;
            Invalidate ();
        }

        // Properties

        // BUG: The term "blink" is a verb. Use Blinking instead.
        // TODO: Move blinking support to the Panel itself.
        // TODO: In category "behavior" ?

        [Category ("LCARS"), Description ("Blink button")]
        public bool Blink
        {
            get
            {
                return this._Blink;
            }
            set
            {
                this._Blink = value;
                this.timer1.Enabled = this._Blink;
            }
        }


        [Description ("Sound file"), Category ("LCARS")]
        public string SoundFile
        {
            get
            {
                return this._SoundFile;
            }
            set
            {
                this._SoundFile = value;
                base.Invalidate ();
            }
        }

    }
}